package com.nciae.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nciae.enums.ProductStatusInfoEnum;
import com.nciae.form.CategoryForm;
import com.nciae.form.ProductInfoForm;
import com.nciae.form.ProductInfoUpdateForm;
import com.nciae.po.ProductCategory;
import com.nciae.po.ProductInfo;
import com.nciae.service.IProductCategoryService;
import com.nciae.service.IProductInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YangChen
 * @since 2021-10-14
 */
@Controller
@RequestMapping("/productInfo")
public class ProductInfoController {

    @Autowired
    private IProductInfoService productInfoService;

    @Autowired
    private IProductCategoryService productCategoryService;




    @GetMapping("/list")
    public ModelAndView list( @RequestParam(value = "page" ,defaultValue = "0") Integer page,
                              @RequestParam(value = "size" ,defaultValue = "5")Integer size,
                              Map<String, Object> map
    ){
        IPage<ProductInfo> pages = productInfoService.findByPage(page, size);
        map.put("productInfoList", pages.getRecords());
        map.put("total", pages.getPages());
        map.put("page",pages.getCurrent());
        map.put("size", pages.getSize());
        return new ModelAndView("/product/list",map);
    }



    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId, Map<String, Object> map) {

        if (productId == null) {
            List<ProductCategory> productCategoryList = productCategoryService.findAll();
            map.put("productCategoryList", productCategoryList);
            return new ModelAndView("product/insert" ,map);
        }
        ProductInfo productInfo = productInfoService.findOne(productId);
        ProductCategory categoryType = productCategoryService.findByCategoryType(productInfo.getCategoryType());
        System.out.println("categoryType:" + categoryType);
        List<ProductCategory> productCategoryList = productCategoryService.findAll();
        map.put("productInfo", productInfo);
        map.put("categoryType", categoryType.getCategoryType());
        map.put("productCategoryList", productCategoryList);
        return new ModelAndView("product/update" ,map);
    }



    @PostMapping("/insert")
    public ModelAndView insert(@Valid ProductInfoForm ProductInfoForm,
                               BindingResult bindingResult,
                               Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productInfo/insert");
            return new ModelAndView("common/error" ,map);
        }
        ProductInfo productInfo = new ProductInfo();
        try {
            BeanUtils.copyProperties(ProductInfoForm, productInfo);
            productInfoService.insert(productInfo);
        }catch (Exception e) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productInfo/insert");
            return new ModelAndView("common/error" ,map);
        }

        map.put("url","/productInfo/list");
        return new ModelAndView("common/success" ,map);
    }


    @PostMapping("/update")
    public ModelAndView update(@Valid ProductInfoUpdateForm productInfoUpdateForm,
                               BindingResult bindingResult,
                               Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productInfo/update");
            return new ModelAndView("common/error" ,map);
        }
        ProductInfo productInfo = new ProductInfo();
        try {
            BeanUtils.copyProperties(productInfoUpdateForm, productInfo);
            productInfoService.update(productInfo);
        }catch (Exception e) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productInfo/update");
            return new ModelAndView("common/error" ,map);
        }

        map.put("url","/productInfo/list");
        return new ModelAndView("common/success" ,map);
    }

    @GetMapping("/status")
    public ModelAndView changeStatus(@RequestParam("productId") String productId,
                                     @RequestParam(value = "page" ,defaultValue = "0") Integer page,
                                     @RequestParam(value = "size" ,defaultValue = "5")Integer size,
                                     Map<String, Object> map) {
        
        ProductInfo productInfo = productInfoService.findOne(productId);
        if(productInfo.getProductStatus().equals(ProductStatusInfoEnum.PUT.getCode())) {
            System.out.println("进来了");
            productInfo.setProductStatus(ProductStatusInfoEnum.OFF.getCode());
        } else {
            productInfo.setProductStatus(ProductStatusInfoEnum.PUT.getCode());
        }
        IPage<ProductInfo> pages = productInfoService.findByPage(page, size);
        map.put("productInfoList", pages.getRecords());
        map.put("total", pages.getPages());
        map.put("page",pages.getCurrent());
        map.put("size", pages.getSize());
        productInfoService.update(productInfo);
        return new ModelAndView("/product/list", map);
    }

}
